package com.android.hedgehog;

import android.graphics.Color;
import android.util.Log;

import com.android.hedgehog.framework.Graphics;
import com.android.hedgehog.framework.Input;

public class DataMatrixScreen extends GameScreen {
	private enum GameState {
		Ready, Running, Input, Result
	}
	Graphics graphics;
	double adjustTime;
	DataMatrixData matrix;
	GameState state;
	int[] input = new int[3];
	double bufferTime = 0;
	float roundTime = 0.0f;
	float yPosition = 0.0f;

	public DataMatrixScreen(Graphics graphics, int difficulty, double time) {
		this.graphics = graphics;
		bufferTime = time;
		matrix = new DataMatrixData(difficulty);
		state = GameState.Ready;
	}

	public int update(int X, int Y, int totalScore, double time) {

		if (time > 2 && state == GameState.Ready) {
			adjustTime = time;
			state = GameState.Running;
		}
		if (state == GameState.Running) {
			roundTime = (float) (time - adjustTime);
		}
		if (state == GameState.Input) {
			if (X > 100 && X < 140 && Y > 300 && Y < 330)
				input[0]++;
			if (X > 140 && X < 180 && Y > 300 && Y < 330)
				input[1]++;
			if (X > 180 && X < 220 && Y > 300 && Y < 330)
				input[2]++;
			if (X > 100 && X < 140 && Y > 390 && Y < 420)
				input[0]--;
			if (X > 140 && X < 180 && Y > 390 && Y < 420)
				input[1]--;
			if (X > 180 && X < 220 && Y > 390 && Y < 420)
				input[2]--;

			for (int i = 0; i < input.length; i++) {
				if (input[i] > 9)
					input[i] = 0;
				if (input[i] < 0)
					input[i] = 9;
			}

			if (X > 245 && X < 295 && Y > 335 && Y < 385) {
				matrix.checkAnswer(input);
				totalScore += matrix.points;
				state = GameState.Result;
				bufferTime = time;
			}

		}// Input
		if (state == GameState.Result && time > (bufferTime + 3)) {
			gameOver = true;
		}

		return totalScore;
	}
	public void present() {
		if (state == GameState.Ready) {
			graphics.drawText("Count the total", 160, 240, 18, Color.WHITE);
		}
		if (state == GameState.Running) {
			for (int i = 0; i < matrix.matrix.length; i++) {// FOR
				for (int j = 0; j < matrix.matrix[i].length; j++) {
					yPosition = (roundTime * 45 * matrix.speed) - (i * 40);
					if (yPosition > 60 && matrix.matrix[i][j] < 2) {
						graphics.drawSpecialText("" + matrix.matrix[i][j],
								(float) ((j * 40) + 22), yPosition, 40, 1,
								Color.GREEN);
					}
				}
			}// FOR

			if (((roundTime * 45 * matrix.speed) - (matrix.matrix.length * 40)) > 480)
				state = GameState.Input;
		}
		if (state == GameState.Input) {
			graphics.drawPixmap(Assets.numberinput, 100, 300);
			for (int i = 0; i < input.length; i++)
				graphics.drawText("" + input[i], (120 + (i * 40)), 370, 40,
						Color.BLACK);
			graphics.drawPixmap(Assets.whitearrowbuttons, 245, 335, 180, 0, 50,
					50);
		}
		if (state == GameState.Result) {
			if (matrix.points > 0)
				graphics.drawText(matrix.message, 160, 220, 18, Color.GREEN);
			else
				graphics.drawText(matrix.message, 160, 220, 18, Color.RED);
			if (matrix.total != matrix.playerGuess)
				graphics.drawText("actual total was " + matrix.total, 160, 260,
						14, Color.WHITE);

		}
	}

}
